import { _decorator, Component, find, tween, Tween, UIOpacity, UITransform, v3, Vec2, Vec3, Widget } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Tile_bA } from '../../ui/tile';
import { Ab_GameFloat_bA } from './float.game';
import { Ab_GamePopup_bA } from './popup.game';
import { Ab_GameGameBoard_bA } from './game.game.board';
import { Ab_GameDrawerStore_bA } from '../structure/drawer.game.store';
import { Ab_GameDrawerStash_bA } from '../structure/drawer.game.stash';
const { ccclass, property } = _decorator;

@ccclass('Ab_GameGameContainer_bA')
export class Ab_GameGameContainer_bA extends Component {
	@property(Ab_GameGameBoard_bA)
	private Ab_board_bA: Ab_GameGameBoard_bA = null;
	@property(Ab_GameDrawerStore_bA)
	private Ab_Dstore_bA: Ab_GameDrawerStore_bA = null;
	@property(Ab_GameDrawerStash_bA)
	private Ab_Dstash_bA: Ab_GameDrawerStash_bA = null;

	private Ab_ui_drawer_container_bA: UITransform;
	private Ab_ui_board_bA: UITransform;
	private Ab_ui_temp_bA: UITransform;

	private Ab_va_drawer_container_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;

	private Ab_listen_list_bA: Array<ReturnType<typeof Ab_DataTransfer_bA.Ab_follow_bA>> = [];

	private Ab_fruit_count_bA: number = 0;

	protected onLoad(){
		this.Ab_init_bA();
		this.Ab_listening_bA();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.node.children.forEach(n=>{
			let wgt = n.getComponent(Widget);
			if(wgt) wgt.updateAlignment();
		});

		this.Ab_va_drawer_container_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA');
		this.Ab_ui_drawer_container_bA.node.setPosition(this.Ab_va_drawer_container_bA.Ab_vec_bA);

		Ab_GameGameBoard_bA.Ab_onSelectTile_bA(this.Ab_onSelectTile_bA.bind(this));
		Ab_GameDrawerStash_bA.Ab_onSelectRecord_bA(this.Ab_onSelectRecordFromStash_bA.bind(this));
		this.Ab_Dstash_bA.Ab_onClean_bA(this.Ab_onDrawerClean_bA.bind(this));
		this.Ab_Dstore_bA.Ab_onClean_bA(this.Ab_onDrawerClean_bA.bind(this));
		this.Ab_Dstore_bA.Ab_onRemoveTiles_bA(this.Ab_onStoreRemoveTiles_bA.bind(this));
		this.Ab_Dstore_bA.Ab_onPadded_bA(this.Ab_onStorePadded_bA.bind(this));
	}

	protected onDestroy(){
		this.Ab_listen_list_bA.forEach(reg=>Ab_DataTransfer_bA.Ab_cancel_bA(reg));
	}

	private Ab_init_bA(){
		this.Ab_ui_drawer_container_bA = this.node.getChildByName('drawer-container').getComponent(UITransform);
		this.Ab_ui_board_bA = this.node.getChildByName('board').getComponent(UITransform);
		this.Ab_ui_temp_bA = this.node.getChildByName('temp').getComponent(UITransform);

		this.Ab_va_drawer_container_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_ui_drawer_container_bA.node);
	}

	private Ab_listening_bA(){
		this.Ab_listen_list_bA = [];

		this.Ab_listen_list_bA.push(
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UIGame_bA', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.Ab_enterDrawer_bA();
				else this.Ab_leaveDrawer_bA();
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_STASH_bA', (_, force)=> this.Ab_stashTile_bA(force) , false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_REVOKE_bA', (_, force)=> this.Ab_revokeTile_bA(force) , false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_SHUFFLE_bA', (_, force)=> this.Ab_shuffle_bA(force) , false)
		);
	}

	private Ab_enterDrawer_bA(duration?: number){
		duration = duration ?? 0.5;

		this.Ab_va_drawer_container_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_bottom_bA');
		this.Ab_va_drawer_container_bA.Ab_vec_bA.add3f(0, 244, 0);

		Tween.stopAllByTarget(this.Ab_ui_drawer_container_bA.node);
		tween(this.Ab_ui_drawer_container_bA.node)
		.to(duration, {position: this.Ab_va_drawer_container_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.start();
	}

	private Ab_leaveDrawer_bA(duration?: number){
		duration = duration ?? 0.5;

		this.Ab_va_drawer_container_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA');

		Tween.stopAllByTarget(this.Ab_ui_drawer_container_bA.node);
		tween(this.Ab_ui_drawer_container_bA.node)
		.to(duration, {position: this.Ab_va_drawer_container_bA.Ab_vec_bA}, {easing: 'cubicIn'})
		.call(()=>{
			this.Ab_Dstash_bA.Ab_clean_bA();
			this.Ab_Dstore_bA.Ab_clean_bA();
		}).start();
	}

	private Ab_onSelectRecordFromStash_bA(record: {tier: number, coor: Vec2, tile: Ab_Tile_bA}){
		if(!(this.Ab_Dstore_bA.Ab_residue_bA>0)) return void 0;
		this.Ab_Dstash_bA.Ab_removeRecord_bA(record.tile);

		Tween.stopAllByTarget(record.tile.node);

		let pading_info = this.Ab_Dstore_bA.Ab_padding_bA(record);
		if(!pading_info) return void 0;

		let target_positon = this.Ab_ui_temp_bA.convertToNodeSpaceAR(pading_info.world_position);
		let position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(record.tile.node.worldPosition);
		record.tile.node.removeFromParent();
		record.tile.node.setParent(this.Ab_ui_temp_bA.node);
		record.tile.node.setPosition(position);

		let duration = Vec3.subtract(v3(), target_positon, position).length() / Math.max(record.tile.Ab_width_bA, record.tile.Ab_height_bA) * 0.06;

		tween(record.tile.node)
		.to(duration, {position: target_positon}, {easing: 'backIn'})
		.call(()=>this.Ab_Dstore_bA.Ab_padded_bA(pading_info.token))
		.start();
	}

	private Ab_onSelectTile_bA(record: {tier: number, coor: Vec2, tile: Ab_Tile_bA}){
		if(!(this.Ab_Dstore_bA.Ab_residue_bA>0)) return void 0;
		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_CLICK_bA);
		this.Ab_board_bA.Ab_removeTile_bA(record.tier, record.tile);

		Tween.stopAllByTarget(record.tile.node);

		let pading_info = this.Ab_Dstore_bA.Ab_padding_bA(record);
		if(!pading_info) return void 0;

		let target_positon = this.Ab_ui_temp_bA.convertToNodeSpaceAR(pading_info.world_position);
		let position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(record.tile.node.worldPosition);
		record.tile.node.removeFromParent();
		record.tile.node.setParent(this.Ab_ui_temp_bA.node);
		record.tile.node.setPosition(position);

		let duration = Vec3.subtract(v3(), target_positon, position).length() / Math.max(record.tile.Ab_width_bA, record.tile.Ab_height_bA) * 0.06;

		tween(record.tile.node)
		.to(duration, {position: target_positon}, {easing: 'backIn'})
		.call(()=>this.Ab_Dstore_bA.Ab_padded_bA(pading_info.token))
		.start();
	}

	private Ab_onStorePadded_bA(record: {tier: number, coor: Vec2, tile: Ab_Tile_bA, token?: string}, full: boolean){
		if(full){
			abi.Ab_log_bA.Ab_tag_bA('GameOver: red');
			this.Ab_GameOver_bA(false);
		}

		//if(Ab_GameDC_bA.Ab_User_bA.Ab_lock_bA) Ab_GameDC_bA.Ab_Trigger_bA.Ab_HintLock_bA = residue<=2;
	}

	private Ab_onDrawerClean_bA(tile_list: Ab_Tile_bA[]){
		tile_list.forEach(t=>Ab_GameGameBoard_bA.Ab_recycleTile_bA(t));
	}

	private async Ab_onStoreRemoveTiles_bA(tile_list: Ab_Tile_bA[], world_position: Vec3){
		tile_list.forEach(t=>Ab_GameGameBoard_bA.Ab_recycleTile_bA(t));

		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_REMOVE_bA);

		Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA++;
		Ab_GameDC_bA.Ab_Status_bA.Ab_removeTileCount_bA += tile_list.length;

		Ab_GameFloat_bA.Ab_XX_bA(world_position, 3);

		if(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA >= 4){
			this.Ab_fruit_count_bA++;
			if(this.Ab_fruit_count_bA==6) Ab_ClientAPI_bA.Ab_ScorePop_bA();
		}

		if(Ab_GameDC_bA.Ab_Status_bA.Ab_removeTileCount_bA==this.Ab_board_bA.Ab_tileCount_bA){
			abi.Ab_log_bA.Ab_tag_bA('GameOver: green');
			this.Ab_GameOver_bA(true);
		}
	}

	private Ab_stashTile_bA(force: boolean){
		let count = Math.min(3, this.Ab_Dstore_bA.Ab_tileCount_bA);
		if(count <= 0){
			Ab_GameFloat_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(63));
			return void 0;
		}

		count = Math.min(count, this.Ab_Dstash_bA.Ab_residue_bA);
		if(count <= 0){
			Ab_GameFloat_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(64));
			return void 0;
		}

		let list = this.Ab_Dstore_bA.Ab_unshiftTile_bA(count);

		if(list.length<=0) return void 0;

		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_REVOKE_bA);

		list.forEach(record=>{
			Tween.stopAllByTarget(record.tile.node);

			let position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(record.tile.node.worldPosition);
			let padding_info = this.Ab_Dstash_bA.Ab_padding_bA(record);
			let target_position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(padding_info.world_position);

			record.tile.node.removeFromParent();
			record.tile.node.setParent(this.Ab_ui_temp_bA.node);
			record.tile.node.setPosition(position);

			let duration = Vec3.subtract(v3(), target_position, position).length() / Math.max(record.tile.Ab_width_bA, record.tile.Ab_height_bA) * 0.06;

			tween(record.tile.node)
			.to(duration, {position: target_position}, {easing: 'cubicOut'})
			.call(()=>this.Ab_Dstash_bA.Ab_padded_bA(padding_info.token))
			.start();
		});

		if(force) return void 0;

		Ab_GameDC_bA.Ab_User_bA.Ab_prop_stash_bA--;
	}

	private Ab_revokeTile_bA(force: boolean){
		let record = this.Ab_Dstore_bA.Ab_popLastRecord_bA();

		if(!record){
			Ab_GameFloat_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(63));
			return void 0;
		}

		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_REVOKE_bA);

		Tween.stopAllByTarget(record.tile.node);

		let position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(record.tile.node.worldPosition);
		let target_position = this.Ab_ui_temp_bA.convertToNodeSpaceAR(this.Ab_board_bA.Ab_appendingTile_bA(record.tier, record.coor, record.tile));

		record.tile.node.removeFromParent();
		record.tile.node.setParent(this.Ab_ui_temp_bA.node);
		record.tile.node.setPosition(position);

		let duration = Vec3.subtract(v3(), target_position, position).length() / Math.max(record.tile.Ab_width_bA, record.tile.Ab_height_bA) * 0.06;

		tween(record.tile.node)
		.to(duration, {position: target_position}, {easing: 'cubicOut'})
		.call(()=>this.Ab_board_bA.Ab_appendedTile_bA(record.tier, record.coor, record.tile))
		.start();

		if(force) return void 0;

		Ab_GameDC_bA.Ab_User_bA.Ab_prop_revoke_bA--;
	}

	private Ab_shuffle_bA(force: boolean){
		this.Ab_board_bA.Ab_shuffle_bA();
		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_SHUFFLE_bA);

		if(force) return void 0;

		Ab_GameDC_bA.Ab_User_bA.Ab_prop_shuffle_bA--;
	}

	private async Ab_GameOver_bA(victory: boolean){
		if(Ab_GameDC_bA.Ab_Status_bA.Ab_gameover_bA!==undefined) return void 0;

		Ab_GameDC_bA.Ab_Trigger_bA.Ab_GameOver_bA = victory;
		Ab_GameDC_bA.Ab_Status_bA.Ab_gameover_bA = victory;
		let round = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;

		let fc = this.Ab_fruit_count_bA;
		this.Ab_fruit_count_bA = 0;
		Ab_AudioManager_bA.Ab_pauseBgm_bA();
		if(victory){
			Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_BGM_VICTORY_bA);
			await Ab_GamePopup_bA.Ab_Victory_bA(round);

			Ab_GameDC_bA.Ab_User_bA.Ab_round_bA = Math.max(1, Math.min(4, round+1));
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIGame_bA = false;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIHome_bA = false;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UITrans_bA = true;
		}else{
			Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_BGM_DEFEAT_bA);

			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIGame_bA = false;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIHome_bA = false;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UITrans_bA = true;
		}
		Ab_AudioManager_bA.Ab_playBGM_bA();
	}
}

